<template>
  <a-modal
    title='编辑服务分类'
    :width='720'
    :visible='visible'
    :confirmLoading='confirmLoading'
    :maskClosable='false'
    :destroyOnClose='true'
    @ok='handleSubmit'
    @cancel='handleCancel'
  >
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-form-item label='技师名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-input
            v-decorator="['name', {rules: [{required: true, min: 2, message: '请输入至少2个字符'}]}]"
          />
        </a-form-item>
        <a-form-item label='联系方式' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-input
            v-decorator="['mobile', {rules: [{required: true, length: 11, message: '请输入11位手机号码'}]}]"
          />
        </a-form-item>
        <a-form-item v-if="!isAdd" label='所属门店' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-select v-decorator="['address_id', { initialValue: -1 }]">
            <a-select-option :value='-1'>请选择</a-select-option>
            <a-select-option
              v-for='(item, index) in addressDataTree'
              :key='index'
              :value='item.value'
            >{{ item.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label='图片' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <SelectImage
            :defaultList='record.image ? [record.image] : []'
            v-decorator="['cover']" />
        </a-form-item>
        <a-form-item
          label='服务图片'
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          extra='建议比例：16：9'
        >
          <SelectImage
            multiple
            :maxNum='4'
            :defaultList='record.work_albums ? record.work_albums : []'
            v-decorator="['work_album', {rules: [{required: true, message: '请至少上传1张服务图片'}]}]"
          />
        </a-form-item>
        <a-form-item label='介绍' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-textarea v-decorator="['describe']" />
        </a-form-item>
        <a-form-item label='门店排名' :labelCol='labelCol' :wrapperCol='wrapperCol' extra='所属门店内排名，请按顺序配置'>
          <a-input-number
            :min='1'
            v-decorator="['sort', {initialValue: 1, rules: [{required: true, message: '请输入至少1个数字'}]}]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from 'lodash'
import * as Api from '@/api/goods/proficient'
import { SelectImage } from '@/components'

export default {
  components: {
    SelectImage
  },
  props: {
    addressDataTree: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      isAdd: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {}
    }
  },
  methods: {

    // 获取当前详情记录
    getDetail(id) {
      this.confirmLoading = true
      Api.detail({ id: id })
        .then(result => {
          // 当前记录
          this.record = result.data.info
          // 设置默认值
          this.setFieldsValue()
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    /**
     * 显示对话框
     */
    edit(record, isAdd) {
      // 显示窗口
      this.visible = true
      this.isAdd = isAdd
      // // 当前分类记录
      // this.record = record
      // // 设置默认值
      // this.setFieldsValue()
      this.getDetail(record.id)
    },

    /**
     * 设置默认值
     */
    setFieldsValue() {
      const { record, form: { setFieldsValue } } = this
      // 设置表单内容
      this.$nextTick(() => {
        setFieldsValue(_.pick(record, ['name', 'address_id', 'describe', 'sort', 'cover', 'service_number', 'mobile', 'work_album']))
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.onFormSubmit(values)
        }
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.confirmLoading = true
      Api.edit({ id: this.record['id'], form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    }
  }
}
</script>
